# bs4练习案例

# 1.获取页面源代码
import requests
from useragent_helper import get_random_ug

url = "https://blog.csdn.net/csdn1561168266/article/details/124549886"
resp = requests.get(url, headers={'User-agent': get_random_ug()})

print(resp)
print(resp.text)


# 2.用bs4解析数据，拿到数据
from bs4 import BeautifulSoup

page = BeautifulSoup(resp.text, features='html.parser')  # 把源代码交给bs4处理，生成对象

table = page.find('table', attrs={})  # 从bs4中查找数据：find & find_all
print(table)

trs = table.find_all('tr')[1:]  # 拿到所有数据行
print(trs)


# 3.拿到数据并存入data.csv
import csv

f = open('data.csv', mode='w', encoding=None, newline='')
writer = csv.writer(f)

for tr in trs:
    tds = tr.find_all('td')
    writer.writerow([td.text for td in tds])
    # td:       <td>需要安装C语言库</td>
    # td.text:  需要安装C语言库

f.close()
print("Over!")
